import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';
import 'package:dk_flutter_module_sale/common/config/page_base_common.dart';
import 'package:dk_flutter_module_sale/config/route/portal_widget_route.dart';
import 'package:dk_flutter_module_sale/portal/api/portal_api.dart';
import 'package:dk_flutter_module_sale/portal/msg/bean/bean_home_msg.dart';
import 'package:dk_flutter_module_sale/r.dart';

@ARoute(url: PortalWidgetRoute.MESSAGE)
class PortalHomeMsg extends CommonScaffoldPage {
  PortalHomeMsg(BoostRouteOption option) : super();

  @override
  DkAppBar getAppBar(CommonPageBloc pageBloc) {
    return DkAppBar(
        DKAppBarParams(
          titleContent: "消息", bottom: PreferredSize(
            preferredSize: Size.fromHeight(10),
            child: Container(
              color: Color(0xffFAFAFD),
              height: 10,
            )),));
  }

  @override
  DkScaffoldOption configDefaultScaffoldOption(DkScaffoldOption option) {
    return option..backgroundColor = Colors.white;
  }

  @override
  void onInitState(CommonPageBloc pageBloc) {
    FlutterBoost.singleton.addBoostContainerLifeCycleObserver((
        ContainerLifeCycle state, BoostContainerSettings settings) {
      if (settings?.name != PortalWidgetRoute.MESSAGE) {
        return;
      }
      switch (state) {
        case ContainerLifeCycle.Init:
          break;
        case ContainerLifeCycle.Appear:
          pageBloc.pageHttpGet(
              path: PortalApi.homeMsgList, widgetBuilderByData: (dynamic data) {
            return getContentWidget(HomeMsgListBean
                .fromJson(data)
                .list);
          });
          break;
        case ContainerLifeCycle.Disappear:
          break;
        case ContainerLifeCycle.Foreground:
          break;
        case ContainerLifeCycle.Background:
          break;
        case ContainerLifeCycle.Destroy:
          FlutterBoost.singleton.observersHolder.removeObserver(this);
          break;
      }
    });
    pageBloc.pageHttpGet(
        path: PortalApi.homeMsgList, widgetBuilderByData: (dynamic data) {
      return getContentWidget(HomeMsgListBean
          .fromJson(data)
          .list);
    });
  }

  Widget getContentWidget(List<HomeMsgBean> list) {
    return listNotEmpty(list)
        ? ListView.builder(
        scrollDirection: Axis.vertical,
        // item 的个数
        itemCount: list.length,
        itemBuilder: (BuildContext context, int index) {
          HomeMsgBean bean = list[index];
          return Column(children: <Widget>[
            ListTile(
                title: Text(bean?.title), // item 标题
                leading: getImage(bean?.imageCode), // item 前置图标
                subtitle: Text(bean?.tag), // item 副标题
                onTap: () => RouteUtil.goPage(bean?.appRouteUrl)),
            Divider(
                height: 2,
                indent: 73,
                color: Color(0xffEBEEEF))
          ]);
        })
        : LogicPage.emptyPage();
  }

  Widget getImage(String imageCode) {
    String imageAssetPath;
    switch (imageCode) {
      case "notice":
        imageAssetPath = R.imagesPortalMessageNoticePng;
        break;
      case "todo":
        imageAssetPath = R.imagesPortalMessageTodoPng;
        break;
      case "newInfo":
        imageAssetPath = R.imagesPortalMessageNewInfoPng;
        break;
      default:
    }
    return Image.asset(imageAssetPath, width: 43, height: 43);
  }

}

///在Flutter中我们能监听的生命周期有以下几种：

///resumed - 应用程序处于可见状态，并且可以响应用户的输入事件。它相当于Android中Activity的onResume。
///inactive - 应用程序处于闲置状态并且没有收到用户的输入事件。这个状态对Android来说是没用的，它只用于iOS。
///paused - 应用程序处于不可见状态，并且不能够响应用户的输入事件。它相当于Android中Activity的onPause。
///suspending - 应用程序将马上被挂起。这个状态对iOS来说没用。
///下面是生命周期： 
///1、初次打开widget时，不执行AppLifecycleState的回调；
///2、按home键或Power键， AppLifecycleState inactive---->AppLifecycleState pause
///3、从后台到前台：AppLifecycleState inactive--->ApplifecycleState resumed
///4、back键退出应用： AppLifecycleState inactive--->AppLifecycleState paused
/*@override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    switch (state) {
      case AppLifecycleState.resumed:
        requestList(bloc);
        break;
      case AppLifecycleState.inactive:
        break;
      case AppLifecycleState.paused:
        break;
      case AppLifecycleState.suspending:
        break;
    }
  }*/
